import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const PressurePage(title: 'Flutter Demo 用法'),
    );
  }
}

class PressurePage extends StatefulWidget {
  const PressurePage({super.key, required this.title});

  final String title;

  @override
  State<PressurePage> createState() => _PressurePageState();
}

class _PressurePageState extends State<PressurePage> {
  bool _testMode1 = false;
  bool _testMode2 = false;
  late WebViewController _controller1;
  late WebViewController _controller2;
  void _setTestMode1(value) {
    setState(() {
      _testMode1 = value;
      // print("设置map1 模式 ${_testMode1}",);
      if(_testMode1){
        _controller1.evaluateJavascript('game.setTestMode(true)');
      }else{
        _controller1.evaluateJavascript('game.setTestMode(false)');
      }
    },);
  }
  void _setTestMode2(value) {
    setState(() {
      _testMode2 = value;
      // print("设置map2 模式 ${_testMode2}",);
      if(_testMode2){
        _controller2?.evaluateJavascript('game.setTestMode(true)');
      }else{
        _controller2?.evaluateJavascript('game.setTestMode(false)');
      }
    },);
  }
  void _refresh1(){
    // print("更新map1 数据");
    _refresh(_controller1);
  }
  void _refresh2(){
    // print("更新map2 数据");
    _refresh(_controller2);
  }
  void _refresh(WebViewController _controller){
    // print("更新map1 数据");
    List<int> numbers = List.generate(1600, (index) => 0);
    Random random = Random();
    int randomNumber = random.nextInt(101);
    int randomIndex = random.nextInt(numbers.length);
    numbers[randomIndex] = randomNumber;

    String jsonString = jsonEncode(numbers);
    _controller?.evaluateJavascript('game.refresh(${jsonString})');
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body:
      Container(
        padding: EdgeInsets.fromLTRB(10, 10, 10, 10),

        child: Container(
          child:
          Column(
            children: [
              Container(
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                  decoration: BoxDecoration(
                    // color: Color(0xFF000000), // 背景颜色
                  ),
                child:
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [

                    Container(
                      width: 200,
                      height: 200,
                      child: WebView(
                         
                        javascriptMode: JavascriptMode.unrestricted,
                        onWebViewCreated: (WebViewController controller){
                          print("loaded1");
                          _controller1=controller;
                          _controller1.loadFlutterAsset("assets/classic/index.html");
                        },
                        onWebResourceError:(WebResourceError e){
                          print("error1");
                        } ,
                      ),
                    ),
                    SizedBox(width: 10,),
                    Container(
                      width: 100,
                      height: 100,
                      child: WebView(
                        javascriptMode: JavascriptMode.unrestricted,
                        onWebViewCreated: (WebViewController controller){
                          print("loaded2");
                          _controller2=controller;
                          _controller2.loadFlutterAsset("assets/classic/index.html");
                        },
                        onWebResourceError:(WebResourceError e){
                          print("error2");
                        } ,
                      ),
                    ),
                    SizedBox(width: 10,),
                    Image.asset(
                      "assets/images/indicator.png", // 替换为你的图片路径
                      height: 100, // 图片高度
                    )
                  ],
                ),
              ),



              SizedBox(height: 20,),
              //控制

              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [

                  Row(
                    children: [
                      Column(
                        children: [
                          Text("左边:",style: TextStyle(
                            fontSize: 20,
                          ),),
                          Text("是否是测试模式:"),
                          Switch(
                            value: _testMode1,
                            onChanged: _setTestMode1,
                            activeTrackColor: Colors.yellow,
                            activeColor: Colors.orange,
                          ),
                          SizedBox(height: 10,),
                          InkWell(
                            child: Text(
                              '设置一次数据',
                              style: TextStyle(
                                color: Color(0xFF5b6a91),
                                fontSize: 15.0,
                              ),
                            ),
                            onTap: _refresh1,
                          ),
                        ],
                      ),

                      SizedBox(width: 10,),

                      Column(
                        children: [
                          Text("右边:",style: TextStyle(
                            fontSize: 20,
                          ),),
                          Text("是否是测试模式:"),
                          Switch(
                            value: _testMode2,
                            onChanged: _setTestMode2,
                            activeTrackColor: Colors.yellow,
                            activeColor: Colors.orange,
                          ),
                          SizedBox(height: 10,),
                          InkWell(
                            child: Text(
                              '设置一次数据',
                              style: TextStyle(
                                color: Color(0xFF5b6a91),
                                fontSize: 15.0,
                              ),
                            ),
                            onTap: _refresh2,
                          ),
                        ],
                      ),
                    ],
                  )

                ],
              )


            ],
          ),
        ),
      )

    );
  }
}
